from BasicLibraries import *
import utils_nc as ut
import regressions as make_model
from scipy.stats import median_test
import make_data_for_paper as mdfp
import differential_behaviour as DFB
import warnings
warnings.filterwarnings("ignore", category=FutureWarning)
warnings.filterwarnings("ignore", category=RuntimeWarning)
warnings.filterwarnings("ignore", category=UserWarning)

'''
This is also an auxiliary function for the main
'''

region_var = 'loc' ## country-fixed effects
environmental_sdgs = [6,7,11,12,13,14,15] ## Environmental SDGs

#===============
SAVEPLOT= False
#===============

#===============
RS = DFB.MakeEffectAnalysis()

SECTORSTOINCLUDE = ['Industrial', 'Material', 'Energy', 'Utilities']

class PARIS():
    def __init__(self):
        pass
    def differential_effort(self, ReducedData, SQ=-1, target_type = 'well_below', estimation_year = 2019, 
                            show_plots=True, causative_only=False,
                            SAVEPLOT=False, plot_name=''):
        target_year = [2030 if estimation_year == 2020 else 2025][0]
        un = mdfp.make_universe_targets_2020(estimation_year, target_type = target_type)
        aligned = un[un.signed == -1] 
        misaligned = un[un.signed == 1]
        un = un[un.horizon_year == target_year]
        
        
        DataType = ReducedData[(ReducedData.rfyear < estimation_year+1) & (ReducedData.rfyear >= 2010)]
        ListOfInitistives = ut.utils().return_Initiatives()
        #======================================================================
        # For the test without non-causative initiatives
        #====================================================================== 
        if causative_only:
            for NC in ['donation & funding', 'volunteerism']:
                s = ut.utils().get_SpecificInitiatives(NC, environmental_sdgs)
                DataType = DataType.drop(columns = s)
                ListOfInitistives.remove(NC)
        
        #======================================================================
        #== Look at the usual sectors
        DataType = DataType[DataType.GICS_level_0.isin(SECTORSTOINCLUDE)]
        #=== Retain the full sample in the observation period
        DTFULL = DataType.copy()
        DataType = DataType[DataType.gvkey.isin(un.gvkey.unique())]
        res = pd.DataFrame()
        years = np.unique(DTFULL.rfyear)
        for i in years:
            tmp = DataType[DataType.rfyear == i]
            tmp.insert(0, 'q', pd.qcut(tmp['firm_size'], 4, labels = False))
            res = pd.concat((res, tmp))
        DataType = res.copy()
        if SQ>=0:
            print('Take Size quartile')
            DataType = DataType[DataType.q == SQ]
        algn_population = DataType[DataType.gvkey.isin(aligned.gvkey)]
        misl_population = DataType[DataType.gvkey.isin(misaligned.gvkey)]
        print('total number of firms', len(algn_population.gvkey.unique())+len(misl_population.gvkey.unique()))
        print('Aligned firms', len(algn_population.gvkey.unique()))
        print('Misaligned firms', len(misl_population.gvkey.unique()))
        #== Show the contribution of the size quartile to the total contribution of the sectors
        print('Observations for aligned population:', len(algn_population))
        print('Observations for misaligned population:', len(misl_population))

        RS.PARISStatistics(DataType, DTFULL, aligned, misaligned, algn_population, misl_population, 
                           show_plot=True, save_plot=True, FIGNAME=plot_name)
        #== Now look at the behavioral differences between the two groups
        XBt, XTp = RS.BehavioralDifference(algn_population, misl_population, environmental_sdgs,ListOfInitistives, \
                                           dpi_ = 100, percentage=True,show_plot=show_plots, save_plot = SAVEPLOT, plot_name=plot_name)
        #== Significance level of the differential effort
        AL = ReducedData[ReducedData.gvkey.isin(aligned.gvkey.unique())]
        ML = ReducedData[ReducedData.gvkey.isin(misaligned.gvkey.unique())]
        action_significance_paris, sdg_significance_paris =  RS.StatSignificance(ListOfInitistives, environmental_sdgs, AL, ML)
        action_significance_paris = action_significance_paris.set_index('action')
        sdg_significance_paris['SDG'] = sdg_significance_paris['SDG'].apply(lambda x: 'SDG '+str(x))
        sdg_significance_paris = sdg_significance_paris.set_index('SDG')
        #== Look at the excess effort
        ini_PR, sdgs_PR = RS.ExcessEffort(XBt, XTp,action_significance_paris, sdg_significance_paris, show_plot=show_plots, save_plot=SAVEPLOT, plot_name=plot_name)
        print('Aligned population number of initiatives:', np.round(algn_population.number_of_initiatives.mean(), 2))
        print('Misaligned population number of initiatives:', np.round(misl_population.number_of_initiatives.mean(), 2))
        print('t-test:', np.round(median_test(algn_population.number_of_initiatives, misl_population.number_of_initiatives)[1], 2))

        #=== Data for the regressions
        DataType = ReducedData[ReducedData.rfyear < estimation_year+1][ReducedData.gvkey.isin(list(aligned.gvkey.unique()) + list(misaligned.gvkey))].copy()
        DataType = DataType[DataType.GICS_level_0.isin(SECTORSTOINCLUDE)]

        return ini_PR, sdgs_PR, DataType, un, algn_population, misl_population
    def full_sample_estimation(self, REGTYPE, DataType, un, em, ini_PR, sdgs_PR, target_type, vars_continous,vars_named, MakeTable=False, table_name=''):
        #================
        estimationMethod = 'RLM'
        #================

        if REGTYPE == 'regression':
            modelTYPE = 'Full'
            sdgsToRun =  environmental_sdgs
            initiativesToRun = 'all'  
            tableF, x, modelALL = make_model.ContinuousAlignment(DataType, un,  target_type, estimationMethod, region_var, initiativesToRun, sdgsToRun,  \
                                em,   vars_continous, vars_named, 
                                  make_table=MakeTable,  table_name=table_name+'_'+modelTYPE+'.tex')
            modelTYPE = 'BAD_PARIS'
            sdgsToRun =  sdgs_PR[sdgs_PR['Excess effort, %'] < 0]['SDGs'].apply(lambda x: int(x.split(' ')[1])).values.tolist()
            initiativesToRun = ini_PR[ini_PR['Excess effort, %'] < 0]['SDGs'].values.tolist()
            tableB, x, modelBAD = make_model.ContinuousAlignment(DataType, un,  target_type, estimationMethod, region_var, initiativesToRun, sdgsToRun,  \
                                                      em,  vars_continous, vars_named, 
                                                      make_table=MakeTable,  table_name=table_name+'_'+modelTYPE+'.tex')
    
            modelTYPE = 'GOOD_PARIS'
            sdgsToRun =  sdgs_PR[sdgs_PR['Excess effort, %'] > 0]['SDGs'].apply(lambda x: int(x.split(' ')[1])).values.tolist()
            initiativesToRun = ini_PR[ini_PR['Excess effort, %'] > 0]['SDGs'].values.tolist()
            tableG, x, modelGOOD = make_model.ContinuousAlignment(DataType, un,  target_type, estimationMethod, region_var, initiativesToRun, sdgsToRun,  \
                                em,  vars_continous, vars_named, 
                                 make_table=MakeTable,  table_name=table_name+'_'+modelTYPE+'.tex')
        elif REGTYPE == 'probit':
            modelTYPE = 'Full'
            sdgsToRun =  environmental_sdgs
            initiativesToRun = 'all'  

            tableF, x, modelALL = make_model.BinaryAlignment(DataType, un,  target_type, estimationMethod, region_var, initiativesToRun, sdgsToRun,  \
                                em,   vars_continous, vars_named, 
                                  make_table=MakeTable,  table_name=table_name+'_'+modelTYPE+'.tex')
            modelTYPE = 'BAD_PARIS'
            sdgsToRun =  sdgs_PR[sdgs_PR['Excess effort, %'] < 0]['SDGs'].apply(lambda x: int(x.split(' ')[1])).values.tolist()
            initiativesToRun = ini_PR[ini_PR['Excess effort, %'] < 0]['SDGs'].values.tolist()
            tableB, x, modelBAD = make_model.BinaryAlignment(DataType, un,  target_type, estimationMethod, region_var, initiativesToRun, sdgsToRun,  \
                                                      em,  vars_continous, vars_named, 
                                                      make_table=MakeTable,  table_name=table_name+'_'+modelTYPE+'.tex')
    
            modelTYPE = 'GOOD_PARIS'
            sdgsToRun =  sdgs_PR[sdgs_PR['Excess effort, %'] > 0]['SDGs'].apply(lambda x: int(x.split(' ')[1])).values.tolist()
            initiativesToRun = ini_PR[ini_PR['Excess effort, %'] > 0]['SDGs'].values.tolist()
            tableG, x, modelGOOD = make_model.BinaryAlignment(DataType, un,  target_type, estimationMethod, region_var, initiativesToRun, sdgsToRun,  \
                                em,  vars_continous, vars_named, 
                                 make_table=MakeTable,  table_name=table_name+'_'+modelTYPE+'.tex')

        return tableF, tableB, tableG, modelALL, modelBAD, modelGOOD


    def full_sample_estimation_withoutSDGs(self, REGTYPE, DataType, un, em, ini_PR, sdgs_PR, target_type, vars_continous,vars_named, MakeTable=False, table_name=''):
        #================
        estimationMethod = 'RLM'
        #================

        if REGTYPE == 'regression':
            modelTYPE = 'Full'
            sdgsToRun_across =  environmental_sdgs
            initiativesToRun = 'all'  
            tableF, x, modelALL = make_model.ContinuousAlignment(DataType, un,  target_type, estimationMethod, region_var, initiativesToRun, sdgsToRun_across,  \
                                em,   vars_continous, vars_named, 
                                  make_table=MakeTable,  table_name=table_name+'_'+modelTYPE+'.tex')
            modelTYPE = 'BAD_PARIS'
            initiativesToRun = ini_PR[ini_PR['Excess effort, %'] < 0]['SDGs'].values.tolist()
            tableB, x, modelBAD = make_model.ContinuousAlignment(DataType, un,  target_type, estimationMethod, region_var, initiativesToRun, sdgsToRun_across,  \
                                                      em,  vars_continous, vars_named, 
                                                      make_table=MakeTable,  table_name=table_name+'_'+modelTYPE+'.tex')
    
            modelTYPE = 'GOOD_PARIS'
            initiativesToRun = ini_PR[ini_PR['Excess effort, %'] > 0]['SDGs'].values.tolist()
            tableG, x, modelGOOD = make_model.ContinuousAlignment(DataType, un,  target_type, estimationMethod, region_var, initiativesToRun, sdgsToRun_across,  \
                                em,  vars_continous, vars_named, 
                                 make_table=MakeTable,  table_name=table_name+'_'+modelTYPE+'.tex')
        elif REGTYPE == 'probit':
            modelTYPE = 'Full'
            sdgsToRun_across =  environmental_sdgs
            initiativesToRun = 'all'  

            tableF, x, modelALL = make_model.BinaryAlignment(DataType, un,  target_type, estimationMethod, region_var, initiativesToRun, sdgsToRun_across,  \
                                em,   vars_continous, vars_named, 
                                  make_table=MakeTable,  table_name=table_name+'_'+modelTYPE+'.tex')
            modelTYPE = 'BAD_PARIS'
            initiativesToRun = ini_PR[ini_PR['Excess effort, %'] < 0]['SDGs'].values.tolist()
            tableB, x, modelBAD = make_model.BinaryAlignment(DataType, un,  target_type, estimationMethod, region_var, initiativesToRun, sdgsToRun_across,  \
                                                      em,  vars_continous, vars_named, 
                                                      make_table=MakeTable,  table_name=table_name+'_'+modelTYPE+'.tex')
    
            modelTYPE = 'GOOD_PARIS'
            initiativesToRun = ini_PR[ini_PR['Excess effort, %'] > 0]['SDGs'].values.tolist()
            tableG, x, modelGOOD = make_model.BinaryAlignment(DataType, un,  target_type, estimationMethod, region_var, initiativesToRun, sdgsToRun_across,  \
                                em,  vars_continous, vars_named, 
                                 make_table=MakeTable,  table_name=table_name+'_'+modelTYPE+'.tex')

        return tableF, tableB, tableG, modelALL, modelBAD, modelGOOD
    
    def full_sample_estimation_withoutActivity(self, REGTYPE, DataType, un, em, ini_PR, sdgs_PR, target_type, vars_continous,vars_named, MakeTable=False, table_name=''):
        #================
        estimationMethod = 'RLM'
        #================

        if REGTYPE == 'regression':
            modelTYPE = 'Full'
            sdgsToRun =  environmental_sdgs
            initiativesToRun = 'all'  
            tableF, x, modelALL = make_model.ContinuousAlignment(DataType, un,  target_type, estimationMethod, region_var, initiativesToRun, sdgsToRun,  \
                                em,   vars_continous, vars_named, 
                                  make_table=MakeTable,  table_name=table_name+'_'+modelTYPE+'.tex')
            modelTYPE = 'BAD_PARIS'
            sdgsToRun =  sdgs_PR[sdgs_PR['Excess effort, %'] < 0]['SDGs'].apply(lambda x: int(x.split(' ')[1])).values.tolist()
            initiativesToRun = 'all' 
            tableB, x, modelBAD = make_model.ContinuousAlignment(DataType, un,  target_type, estimationMethod, region_var, initiativesToRun, sdgsToRun,  \
                                                      em,  vars_continous, vars_named, 
                                                      make_table=MakeTable,  table_name=table_name+'_'+modelTYPE+'.tex')
    
            modelTYPE = 'GOOD_PARIS'
            sdgsToRun =  sdgs_PR[sdgs_PR['Excess effort, %'] > 0]['SDGs'].apply(lambda x: int(x.split(' ')[1])).values.tolist()
            initiativesToRun = 'all' 
            tableG, x, modelGOOD = make_model.ContinuousAlignment(DataType, un,  target_type, estimationMethod, region_var, initiativesToRun, sdgsToRun,  \
                                em,  vars_continous, vars_named, 
                                 make_table=MakeTable,  table_name=table_name+'_'+modelTYPE+'.tex')
        elif REGTYPE == 'probit':
            modelTYPE = 'Full'
            sdgsToRun =  environmental_sdgs
            initiativesToRun = 'all'  

            tableF, x, modelALL = make_model.BinaryAlignment(DataType, un,  target_type, estimationMethod, region_var, initiativesToRun, sdgsToRun,  \
                                em,   vars_continous, vars_named, 
                                  make_table=MakeTable,  table_name=table_name+'_'+modelTYPE+'.tex')
            modelTYPE = 'BAD_PARIS'
            sdgsToRun =  sdgs_PR[sdgs_PR['Excess effort, %'] < 0]['SDGs'].apply(lambda x: int(x.split(' ')[1])).values.tolist()
            initiativesToRun = 'all' 
            tableB, x, modelBAD = make_model.BinaryAlignment(DataType, un,  target_type, estimationMethod, region_var, initiativesToRun, sdgsToRun,  \
                                                      em,  vars_continous, vars_named, 
                                                      make_table=MakeTable,  table_name=table_name+'_'+modelTYPE+'.tex')
    
            modelTYPE = 'GOOD_PARIS'
            sdgsToRun =  sdgs_PR[sdgs_PR['Excess effort, %'] > 0]['SDGs'].apply(lambda x: int(x.split(' ')[1])).values.tolist()
            initiativesToRun = 'all' 
            tableG, x, modelGOOD = make_model.BinaryAlignment(DataType, un,  target_type, estimationMethod, region_var, initiativesToRun, sdgsToRun,  \
                                em,  vars_continous, vars_named, 
                                 make_table=MakeTable,  table_name=table_name+'_'+modelTYPE+'.tex')

        return tableF, tableB, tableG, modelALL, modelBAD, modelGOOD

    